package server;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Database Singleton class is responsible for instantiating itself and creating
 * a database connection using connector/j. Once connection is made, it is
 * possible to perform queries to the database using query methods
 * 
 * @author Donovan Hatch
 * @version 1.0
 * 
 */
public class Database {

    /**
     * Connection to the database
     */
    private Connection      connection = null;

    /**
     * Instance of the database object
     */
    private static Database Instance   = null;

    /**
     * private Constructor
     * 
     * Creates and sets a database connection
     */
    private Database() {

        // Creates connection to database
        try {
            Class.forName("com.mysql.jdbc.Driver");
            final String url = "jdbc:mysql://db4free.net/gtlocation";
            connection = DriverManager.getConnection(url, "gtlocation", "locationgroup");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * getInstance creates and returns only 1 single Database class instance
     * 
     * @return instance single instance of the Database class
     */
    public static Database getInstance() {
        if (Instance == null) {
            Instance = new Database();
        }
        return Instance;
    }

    /**
     * Query Select is capable of performing Select queries upon the database.
     * This includes: Select does not include: Update, Insert, Delete
     * 
     * @param query
     *            Query requested
     * @return rs Results from the submitted query
     */
    public ResultSet querySelect(String query) {
        Statement stmt = null;
        ResultSet rs = null;

        try {
            stmt = connection.createStatement();
            rs = stmt.executeQuery(query);

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        }
        return rs;

    }

    /**
     * QueryUpdate is capable of performing any queries upon the database that
     * require modification of the table in any form or fashion. this includes:
     * Insert,Update,Delete Does not include: Select
     * 
     * @param query
     * @return id id of the query performed in the database
     */
    public void queryUpdate(String query) {
        Statement stmt = null;

        try {
            stmt = connection.createStatement();
            stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        }
    }

    /**
     * Creates a string representation of the object In this instance, it says
     * if it's connected to the database
     * 
     * @return String says if currently connected to database
     */
    public String toString() {
        if (Database.Instance == null) {
            return "Currently not connected to the database";
        } else {
            return "Currently connected to the database";
        }
    }
}